package fr.umlv.ig.xml;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import fr.umlv.ig.model.Itinerary;
import fr.umlv.ig.model.Line;
import fr.umlv.ig.model.MetroModelImpl;
import fr.umlv.ig.model.Station;

public class SaveXml {
	private PrintWriter printWriter;
	private final FileOutputStream file;
	private final MetroModelImpl metroModel;
	
	public SaveXml(MetroModelImpl metroModel, File name) throws IOException {
		this.metroModel = metroModel;
		
		System.out.println("SaveXml.SaveXml()" + name);

		file = new FileOutputStream(name.getName());
		printWriter = new PrintWriter(new BufferedWriter (new OutputStreamWriter(file, "utf8")));
	}
	
	public PrintWriter getPrintWriter() {
		return printWriter;
	}
	
	public void save() {
		
		printWriter.println("<metroApplication>");
		
		for(int NumberOfLine=0; NumberOfLine<metroModel.getCountLine(); NumberOfLine++) {
			printWriter.println("	<line id=\"" + (NumberOfLine+1) + "\"> ");
			printWriter.println("		<stations>");
			
			Line line = metroModel.getLine(NumberOfLine);
			StringBuilder stationNameBuilder = new StringBuilder();
			for(int indexListStation=0; indexListStation<metroModel.getCountStation(line); indexListStation++) {
				Station station = metroModel.getStation(line, indexListStation);
				stationNameBuilder.append("				<station name=\"");
				stationNameBuilder.append(station.getName());
				stationNameBuilder.append("\" x=\"");
				stationNameBuilder.append(metroModel.getX(station));
				stationNameBuilder.append("\" y=\"");
				stationNameBuilder.append(metroModel.getY(station));
				stationNameBuilder.append("\" time=\"");
				stationNameBuilder.append(metroModel.getWaitingTime(station));
				stationNameBuilder.append("\" />");				
				printWriter.println(stationNameBuilder.toString());
				stationNameBuilder.setLength(0);
			}
			printWriter.println("		</stations>");
			printWriter.println("		<itineraries>");
			StringBuilder itineraryBuilder = new StringBuilder();
			for(int numberOfItinerary=0; numberOfItinerary<metroModel.getCountItinerary(line); numberOfItinerary++) {
				Itinerary itinerary = metroModel.getItinerary(line, numberOfItinerary);
				itineraryBuilder.append("				<itinerary id=\"");
				itineraryBuilder.append(numberOfItinerary);
				itineraryBuilder.append("\" begin=\"");
				itineraryBuilder.append(metroModel.getStart(itinerary).getName());
				itineraryBuilder.append("\" end=\"");
				itineraryBuilder.append(metroModel.getEnd(itinerary).getName());
				itineraryBuilder.append("\" />");
				printWriter.println(itineraryBuilder);
				itineraryBuilder.setLength(0);
			}
			printWriter.println("		</itineraries>");
			printWriter.println("		<edges>");

		
			
			StringBuilder edgeBuilder = new StringBuilder();
			for(int numberOfItinerary=0; numberOfItinerary<metroModel.getCountItinerary(line); numberOfItinerary++) {
				Itinerary itinerary = metroModel.getItinerary(line, numberOfItinerary);
				edgeBuilder.append("			<edgeConfig itinary=\"");
				edgeBuilder.append(numberOfItinerary);
				edgeBuilder.append("\">");
				printWriter.println(edgeBuilder);
				edgeBuilder.setLength(0);
				

				Station currentStation = metroModel.getStart(itinerary);
				Station terminus = metroModel.getEnd(itinerary);
				Station nextStation = metroModel.getNextStation(currentStation, terminus);
				
				System.out.println("SaveXml.save() currentStation : " + currentStation.getName());
				System.out.println("SaveXml.save() terminus : " + terminus.getName());
				System.out.println("SaveXml.save() nextStation : " + nextStation.getName());
				
				if(numberOfItinerary%2 == 1) {
					currentStation = metroModel.getEnd(itinerary);
					terminus = metroModel.getStart(itinerary);
					nextStation = metroModel.getNextStation(currentStation, terminus);
				}
				
				StringBuilder edgeStationBuilder = new StringBuilder();
				while(!currentStation.equals(terminus)){
					
					edgeStationBuilder.append("					<edge station=\"");
					edgeStationBuilder.append(currentStation.getName());
					edgeStationBuilder.append("\" next=\"");
					edgeStationBuilder.append(nextStation.getName());
					edgeStationBuilder.append("\" time=\"");
					edgeStationBuilder.append(metroModel.getWaitingTime(currentStation, nextStation));
					edgeStationBuilder.append("\" />");
					printWriter.println(edgeStationBuilder);
					edgeStationBuilder.setLength(0);
					currentStation = nextStation;
					nextStation = metroModel.getNextStation(currentStation, terminus);
				}
				printWriter.println("			</edgeConfig>");
			}
			printWriter.println("		</edges>");
			printWriter.println("	</line>");
		}
		printWriter.println("</metroApplication>");
		printWriter.close();

	}

}
